<?php


namespace app\api\controller;

use app\api\service\LoginService;
use app\api\service\UserAmountService;
use app\BaseController;
use app\Response;
use think\facade\Request;
use app\api\service\UserService;
use app\api\service\UserWithdrawalService;


class UserWithdrawal extends BaseController
{
    /*** 提现详情
     * @return \think\response\Json
    */
    public function withdrawalView()
    {
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();

            $result = UserWithdrawalService::withdrawalViewService($user_id);
            return $result;
        }
    }

    /*** 提现记录
     * @return \think\response\Json
     */
    public function withdrawalData(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::param("page",1);
            $num = Request::param("num",10);
            $type = Request::param("type","");
            if($type == 1){
                list($list,$count) = UserWithdrawalService::withdrawalDataService($user_id,$page,$num,$type);
                return Response::wrapData($list,$count);
            }elseif($type == 0){
                $data["page"] = $page;
                $data["num"] = $num;
                $data["type"] = $type;
                $res = UserAmountService::userDetailedService($user_id,$data);
                return $res;
            }elseif($type == ""){
                return Response::wrapData([],0);
            }

        }
    }

    //余额提现
    public function withdrawalAdd(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();
            try{
                $this->validate($data, 'UserWithdrawal');
            }catch (\Exception $e){
                return Response::wrapError(0,"参数异常");
            }
            $result = UserWithdrawalService::withdrawalService($user_id,$data);
            return $result;
        }
    }


    /*** 提现条件获取
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function withdrawalState(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();

            $result = UserWithdrawalService::withdrawalStateService($user_id);
            return $result;
        }
    }



}